package cn.ecnu.mapper;

import cn.ecnu.pojo.ConsultRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ConsultRecordMapper {

    /**
     * 根据访客ID, 获取它的咨询记录
     *
     * @param visitorId
     * @return
     */
    @Select("SELECT * FROM psychology.consult_record WHERE from_id = #{id}")
    List<ConsultRecord> getConsultRecordByVisitorId(@Param("id") Integer visitorId);

    /**
     * 修改咨询记录
     *
     * @param consultRecord
     * @return
     */
    int updateRecordById(ConsultRecord consultRecord);

    /**
     * 新增一个咨询记录
     *
     * @param consultRecord
     * @return
     */
    int insertConsultRecord(ConsultRecord consultRecord);

    @Delete("DELETE FROM psychology.consult_record WHERE consult_record_id = #{id}")
    int deleteConsultRecordById(@Param("id") Integer consultRecordId);

    /**
     * 根据咨询师的ID, 获取他的咨询记录
     *
     * @param consultantId
     * @return
     */
    @Select("SELECT * FROM psychology.consult_record WHERE to_id = #{id}")
    List<ConsultRecord> getConsultRecordByConsultantId(@Param("id") Integer consultantId);

    /**
     * 根据督导ID, 获取和他关联的咨询师的咨询记录
     *
     * @param supervisorId
     * @return
     */
    List<ConsultRecord> getConsultRecordBySupervisorId(@Param("id") Integer supervisorId);

    /**
     * 根据咨询记录ID获取对应的咨询记录
     *
     * @param consultRecordId 咨询记录ID
     * @return 咨询记录
     */
    @Select("SELECT * FROM psychology.consult_record WHERE consult_record_id = #{id}")
    ConsultRecord getConsultRecordById(@Param("id") Integer consultRecordId);

    // 获取所有的咨询记录
    @Select("SELECT * FROM psychology.consult_record")
    List<ConsultRecord> getAllConsultRecord();

    // 某个咨询师今天完成的咨询数
    @Select("select count(*) from consult_record where date(datetime)=curdate() and to_id=#{uid}")
    Integer getCountToday(Integer uid);

    // 某个咨询师今天完成的咨询时长
    @Select("select sum(duration) from consult_record where date(datetime)=curdate() and to_id=#{uid}")
    Integer getTimeToday(Integer uid);

    // 今天完成的所有咨询数
    @Select("select count(*) from consult_record where date(datetime)=curdate()")
    Integer getAllCountToday();

    // 今天完成的咨询时长总和
    @Select("select sum(consult_record.duration) from consult_record where date(datetime)=curdate()")
    Integer getAllTimeToday();

    // 获取今天某个小时的咨询数量
    @Select("SELECT count(*) FROM psychology.consult_record where date(datetime)=curdate() and hour(datetime)=#{hour}")
    Integer getTodayHourCount(Integer hour);

    // 获取若干天前的某天的所有咨询数
    @Select("SELECT count(*) FROM psychology.consult_record where date(datetime)=date_sub(curdate(), interval #{num} day)")
    Integer getDayCountByGap(Integer num);

}








